package com.yidu.ruifeng_logistics.controller.loginOrRegister;

import com.yidu.ruifeng_logistics.entity.LogisticsClient;
import com.yidu.ruifeng_logistics.service.LogisticsClientService;
import com.yidu.ruifeng_logistics.service.LogisticsStaffService;
import com.yidu.ruifeng_logistics.utils.Admin;
import com.yidu.ruifeng_logistics.utils.BaseMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: 肖楚科
 * @date: 2021/3/11
 * @time: 19:47
 * @blog: com.yidu.ruifeng_logistics.controller.loginOrRegister
 * @description: 登陆注册
 */
@Api(description = "仓库接口")
@RestController
@RequestMapping("loginOrRegister")
public class LoginOrRegisterController {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    LogisticsStaffService logisticsStaffService;
    @Resource
    LogisticsClientService logisticsClientService;

    /**
     * 登录功能
     * @param choose 身份选择
     * @param userName 用户名
     * @param userPwd 密码
     * @param session sesion
     * @return 状态信息
     */
    @ApiOperation(value = "登陆验证",notes = "返回状态信息")
    @PostMapping("/check")
    public Map<String, Object> login(String choose, String userName, String userPwd, HttpSession session){
        Map<String, Object> map = new HashMap<>();
        //根据身份选择判断，s为员工
        if ("s".equals(choose)) {
            //判断账号密码是否与配置文件中的默认管理员账号匹配
            if (Admin.userAdmin.equals(userName) && Admin.userPassword.equals(userPwd)) {
                //identity表示身份的session，login为该用户的对象
                session.setAttribute("identity","staff");
                //根据账号密码查询员工对象，生成session
                session.setAttribute("login", logisticsStaffService.queryUserNameAndPwdReturnObj(userName,userPwd));
                //传递status与msg状态信息
                map.put("status",2);
                map.put("msg","默认管理员");
                //根据账号密码查询员工的行数，判断是否存在
            }else if (logisticsStaffService.queryUserNameAndPwdReturnCount(userName,userPwd)>0){
                session.setAttribute("identity","staff");
                //根据账号密码查询员工对象，生成session
                session.setAttribute("login",logisticsStaffService.queryUserNameAndPwdReturnObj(userName,userPwd));
                map.put("status",1);
                map.put("msg","员工");
                //data为根据账号密码查询出来的员工对象
                map.put("data",logisticsStaffService.queryUserNameAndPwdReturnObj(userName,userPwd));
            }else {
                //账号密码错误的情况
                map.put("status",0);
                map.put("msg","账号或密码错误");
            }
            //c为客户
        }else if ("c".equals(choose)){
            //判断客户的账号密码
            if (logisticsClientService.getSession(userName,userPwd)!=null){
                session.setAttribute("identity","client");
                //根据客户账号密码查询，生成session所需对象
                session.setAttribute("login",logisticsClientService.getSession(userName,userPwd));
                map.put("status",1);
                map.put("msg","客户");
                //data为根据账号密码查询出来的客户对象
                map.put("data",logisticsClientService.getSession(userName,userPwd));
            }else {
                //账号密码错误的情况
                map.put("status",0);
                map.put("msg","账号或密码错误");
            }
        }
        session.setAttribute("userName",userName);
        return map;
    }

    /**
     * 注册功能
     * @param logisticsClient 客户
     * @return 状态信息
     */
    @ApiOperation(value = "客户注册",notes = "返回状态信息")
    @PostMapping("/clientReg")
    public BaseMessage clientReg(LogisticsClient logisticsClient){
        //根据手机号查询客户，判断该账号是否已被注册
        if (logisticsClientService.checkByTeL(logisticsClient.getClientTel())>0){
            //被注册过就返回失败的状态信息
            return BaseMessage.fail();
        }else {
            //没被注册就增加客户
            logisticsClientService.insert(logisticsClient);
            return BaseMessage.success();
        }
    }
}